package pers.chao.nacos;

import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
import org.slf4j.Logger;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pers.chao.tool.date.DatePattern;
import pers.chao.tool.date.EasyDateFormat;
import pers.chao.tool.infrastructure.log.BaseLogFactory;
import pers.chao.tool.infrastructure.resource.ResUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Description: 自动配置类
 *
 * @author W.Chao
 * @date 2021/1/2 0:39
 **/
@Configuration
@ConditionalOnNacosDiscoveryEnabled
@ConditionalOnProperty(value = {"spring.cloud.nacos.discovery.watch.enabled"}, matchIfMissing = true)
@AutoConfigureBefore({NacosDiscoveryAutoConfiguration.class})
public class NacosServiceMetadataAutoConfigure {
	private static final Logger logger = BaseLogFactory.getLogger(NacosServiceMetadataAutoConfigure.class);

	@Bean
	public NacosDiscoveryProperties nacosDiscoveryProperties() {
		NacosDiscoveryProperties nacosDiscoveryProperties = new NacosDiscoveryProperties();
		// 获取打包构建时的git信息
		Properties gitMetaProperties = getGitMetaProperties();
		Map<String, String> meta = new HashMap<>();
		gitMetaProperties.forEach((k, v) -> meta.put((String) k, (String) v));
		// 更改服务详情中的元数据
		nacosDiscoveryProperties.setMetadata(meta);
		return nacosDiscoveryProperties;
	}

	/**
	 * git.commit.id.abbrev=e6ff0fe
	 * git.commit.user.email=xxx@xxx
	 * git.commit.message.full=[2020/12/30]\n1.a-kit-util\n
	 * git.commit.id=e6ff0fe16f346355a81f3d37804c9f4b2a6b600c
	 * git.commit.message.short=[2020/12/30] 1.a-kit-util包名更新为pers
	 * git.commit.user.name=chaooWoo
	 * git.build.user.name=chaooWoo
	 * git.commit.id.describe=e6ff0fe-dirty
	 * git.build.user.email=xxxx@qq.com
	 * git.branch=master
	 * git.commit.time=20201231002930
	 * git.build.time=20210102010429
	 * git.remote.origin.url=https\://gitee.com/woo_chaos/response-boxing-starter.git
	 *
	 * @return 部分git属性作为服务元数据上报
	 */
	private Properties getGitMetaProperties() {
		String gitFileClassPath = "git.properties";
		String gitCommitIdAbbrev = "git.commit.id.abbrev";
		String gitCommitId = "git.commit.id";
		String gitCommitTime = "git.commit.time";
		String gitBuildTime = "git.build.time";
		String gitBranch = "git.branch";
		Properties metadata = new Properties();
		metadata.put("startup.build.time", EasyDateFormat.format(new Date(), DatePattern.yyyyMMddTHHmmssZ.getPattern()));
		try {
			Properties gitProperty = ResUtil.getProperties(gitFileClassPath);
			metadata.put(gitCommitIdAbbrev, gitProperty.getProperty(gitCommitIdAbbrev, "null"));
			metadata.put(gitCommitId, gitProperty.getProperty(gitCommitId, "null"));
			metadata.put(gitCommitTime, gitProperty.getProperty(gitCommitTime, "null"));
			metadata.put(gitBuildTime, gitProperty.getProperty(gitBuildTime, "null"));
			metadata.put(gitBranch, gitProperty.getProperty(gitBranch, "null"));
		} catch (Exception e) {
			throw new AnalysisGitMetadataPropertiesException("Analysis git metadata error. Check /src/main/resources/git.properties, current: {}", metadata.toString(), e);
		}
		logger.info("Analysis git metadata: {}", metadata.toString());
		return metadata;
	}
}
